/*********************************************************************************
Organization 					: 				Georgia Institute of Technology
  
Institute 						:				Cognitive Computing Group(CCL)
 
Authors							: 				Santiago Ontanon, Kinshuk Mishra
 												Neha Sugandh 
 												
Class							:				DefeatPlayerGoal
 
Function						: 				Provides a class for holding the 
												defeat player goal
****************************************************************************/
package planninglanguage.goals;

import org.jdom.Element;
import org.w3c.dom.Node;

import planninglanguage.conditions.Condition;
import planninglanguage.conditions.NotCondition;
import planninglanguage.conditions.PlayerExistsCondition;
import planninglanguage.softconditions.PlayerDefeatedSoftCondition;
import planninglanguage.softconditions.SoftCondition;
import util.XMLWriter;
import base.WargusGameState;

public class DefeatPlayerGoal extends Goal implements Cloneable{
	
	int m_selfID;
	int m_enemyID;
	
	public DefeatPlayerGoal(int a_selfID,int a_enemyID)
	{
		m_selfID = a_selfID;
		m_enemyID = a_enemyID;
	}
	public int unitID()
	{
		return m_enemyID;
	}
	public Object clone()
	{
		return new DefeatPlayerGoal(m_selfID,m_enemyID);
	}
	
	public Condition generateSuccessCondition(Node a_gamestate)
	{
		return new NotCondition(new PlayerExistsCondition(m_enemyID));
	}
	
	public SoftCondition generateSuccessSoftCondition(WargusGameState a_gamestate)
	{
		return new PlayerDefeatedSoftCondition(m_selfID,m_enemyID,
				a_gamestate.getPlayerStrength(m_selfID),a_gamestate.getPlayerStrength(m_enemyID));		
	}	
	
	public float distance(Goal a_g) {
		if (!(a_g instanceof DefeatPlayerGoal)) return 1;

		DefeatPlayerGoal g = (DefeatPlayerGoal)a_g;
		float dp = 1;
		
		if (m_enemyID == g.m_enemyID) dp = 0;
		
		return dp*0.5f;	/* values are between 0 and 0.5 since at least the goal type is the same */ 
	}	
	
	public String toString() {
		return "DefeatPlayerGoal( self = " + m_selfID + ", enemy = " + m_enemyID + ")";
	}	
	
	public void writeXML(XMLWriter w) {
		w.tag(Goal.openTag + " " + Goal.XMLtype + "=\"" + 
				this.getClass().getSimpleName() + "\"");
		{
			w.tag("enemy-id", m_enemyID);
			w.tag("self-id", m_selfID);
		}
		w.tag(Goal.closeTag);
	}
	protected static DefeatPlayerGoal loadFromXML(Element e) {
		int selfID = Integer.parseInt(e.getChildText("self-id")); 
		int enemyID = Integer.parseInt(e.getChildText("enemy-id"));
		return new DefeatPlayerGoal(selfID, enemyID);
	}
}
